#include "MainHeader.h"
int SetupDoors() {
	long area;
	int i=0;
	char tbuf[5];
	//If File = "ZM" Then
	
	
	area = Combos[cArea].GetListIndex();
	//&h55 Doors for 6th area
	
	if(GameEngine->isZM()){
		//if(area==5){
            // DoorNum = 0x35;
		//}else 
			if (area == 6) {
			DoorNum = 0xF5;
		}else{
			DoorNum = (DoorOffsets[area + 1] - DoorOffsets[area]) / 12;
		}
	}
	if(GameEngine->isMF()){
		if (area == 9){
			DoorNum = 0x3;
		}else if(area == 4){
		    DoorNum = 0x77;
		}else{

			DoorNum = (DoorOffsets[area + 1] - DoorOffsets[area]) / 12;
		}
	}
	Combos[cD1].Clear();
	Combos[cD2].Clear();
	for(i=0;i<DoorNum;i++){
		
		sprintf(tbuf,"%X",i);
		Combos[cD1].Additem(tbuf);
		Combos[cD2].Additem(tbuf);
	}
	GameEngine->OpenFile();
	FILE* fp=GameEngine->oFP();
		if(fp){
	
	 fseek(fp,DoorOffsets[Combos[cArea].GetListIndex()] - 0x8000000,SEEK_SET);

	for(i =0; i <  DoorNum;i++){
		fread(&Doors[i].DoorType,sizeof(u8),1,fp);
		fread(&Doors[i].OwnerRoom,sizeof(u8),1,fp);
		fread(&Doors[i].XEntrance,sizeof(u8),1,fp);
		fread(&Doors[i].DWidth,sizeof(u8),1,fp);
		
		fread(&Doors[i].YEntrance,sizeof(u8),1,fp);
		fread(&Doors[i].DHeight,sizeof(u8),1,fp);
		
		fread(&Doors[i].DestDoor,sizeof(u8),1,fp);
		fread(&Doors[i].ExitDoorLength,sizeof(u8),1,fp);
		fread(&Doors[i].u1,sizeof(u8),1,fp);
		fread(&Doors[i].u2,sizeof(u8),1,fp);
		fread(&Doors[i].u3,sizeof(u8),1,fp);
		fread(&Doors[i].u4,sizeof(u8),1,fp);
		
	}
	
		}
	//Writing to a file;
		
	/*	FILE * fp = fopen("C:\\Documents and Settings\\Matt\\Desktop\\doors.txt","w");
		if(fp){
		char debugbuf[256];
		for(i=0;i<DoorNum;i++){

          sprintf(debugbuf,"*********************Door %X********************\r\n",i);
		  	 fputs(debugbuf,fp);
         sprintf(debugbuf,"Door Type %X\r\n", Doors[i].DoorType);
		 	 fputs(debugbuf,fp);
         sprintf(debugbuf,"Owner Room %X\r\n",Doors[i].OwnerRoom);
		 	 fputs(debugbuf,fp);
		 sprintf(debugbuf,"X Entrance %X\r\n",Doors[i].XEntrance);
		 	 fputs(debugbuf,fp);

		 sprintf(debugbuf,"Door Width %X\r\n",Doors[i].DWidth);
		 	 fputs(debugbuf,fp);
		 sprintf(debugbuf,"Y Entrance %X\r\n",Doors[i].YEntrance);
		 	 fputs(debugbuf,fp);

		 sprintf(debugbuf,"Door Height %X\r\n",Doors[i].DHeight);
		 	 fputs(debugbuf,fp);
	
		

		 sprintf(debugbuf,"Destination Door %X\r\n", Doors[i].DestDoor);
		 	 fputs(debugbuf,fp);
	    
		 sprintf(debugbuf,"Exit Length %X\r\n",Doors[i].ExitDoorLength);
		 	 fputs(debugbuf,fp);
		 sprintf(debugbuf,"Door Unknowns: %X,%X,%X,%X\r\n",Doors[i].u1,Doors[i].u2,Doors[i].u3,Doors[i].u4);
		 	 fputs(debugbuf,fp);
	     
          sprintf(debugbuf,"************************************************\r\n");
		  	 fputs(debugbuf,fp);
		}
		fclose(fp);
		}else{
			MessageBox(0,"Lame",NULL,MB_OK);
		}*/
	return 0;
} 

int LoadDoors(){
	
	int i=0;

	
	for (i = 0; i <DoorBuf.DoorCount;i++)  DoorBuf.DoorNotes[i] = 0;
	
	DoorBuf.DoorCount = 0;
	for(i = 0; i<DoorNum;i++){
		if ((Doors[i].OwnerRoom == Combos[cRoom].GetListIndex())) {
			
			DoorBuf.DoorNotes[DoorBuf.DoorCount] = i;
			
			DoorBuf.DoorCount +=  1;
		}
	}
	

	return 0;
}

int DisplayDoors(){ 
	int minX; 
	int maxX;
	int minY;
	int maxY;		
	int curW,curH,curX,curY;
	int i=0;			 
	char cBuf[512]={0};
	
	
	//First we get the current display area Displays 23x 21 Tiles at a time
	//so Get scrollbar for basics then get add proper constant

	
	minX = nHScroll[sHMap];
	maxX = nHScroll[sHMap] + 23;
	minY = nVScroll[sVMap];
	maxY = nVScroll[sVMap]+21;
	u32 que = 0;
	///RECT blah={0,0,0,0};
	 SetBkMode(ThisBackBuffer.DC(), TRANSPARENT);
	for(i = 0; i<DoorBuf.DoorCount;i++){
		que = Combos[cRoom].GetListIndex();
		if(Doors[DoorBuf.DoorNotes[i]].OwnerRoom == Combos[cRoom].GetListIndex()){
			curX = Doors[DoorBuf.DoorNotes[i]].XEntrance;
			curY =	Doors[DoorBuf.DoorNotes[i]].YEntrance;
			curH  = (Doors[DoorBuf.DoorNotes[i]].DHeight);//-Doors[DoorBuf.DoorNotes[i]].YEntrance)+1;
			curW = (Doors[DoorBuf.DoorNotes[i]].DWidth);//-Doors[DoorBuf.DoorNotes[i]].XEntrance)+1;
		//	ShowWindow((HWND)hDoors[i],SW_SHOW);
		//	MoveWindow((HWND)hDoors[i],(u16)(curX-minX)*16,(u16)(curY-minY)*16,(curW+1)*16,(curH-curY+1)*16,0);
			RECT blah;
			for(int d=0;d<2;d++){
				blah.left = (curX)*16+d;
				blah.top = (curY)*16+d;
				blah.right=(curW+1)*16+d;
				blah.bottom=(curH+1)*16+d;
					HBRUSH curbrush = CreateSolidBrush(RGB(255, 255, 255));
					
                    FrameRect(ThisBackBuffer.DC(), &blah,curbrush);
	                 DeleteObject(curbrush);
			}
		
					 TextOut(ThisBackBuffer.DC(),
						 ((curX) + (((curW-curX)/2)))*(16),
						 ((curY) + (((curH-curY)/2)))*(16),"D",1);
		}
		
		
	}
	
	return 0; 
}

int SaveDoors(){
	
	u8 h=0;
	int i=0;
	GameEngine->OpenFile();
	FILE *fp = GameEngine->oFP();
	
		fseek(fp,DoorOffsets[Combos[cArea].GetListIndex()] - 0x8000000,SEEK_SET);

	
	
	for( i =0; i <	DoorNum;i++){
		fwrite(&Doors[i].DoorType,sizeof(u8),1,fp);
		fwrite(&Doors[i].OwnerRoom,sizeof(u8),1,fp);
		fwrite(&Doors[i].XEntrance,sizeof(u8),1,fp);
		// Doors[i].DWidth +=	Doors[i].XEntrance/16;
		// h = Doors[i].DWidth  + Doors[i].XEntrance-1;
		fwrite(&Doors[i].DWidth,sizeof(u8),1,fp);
		
		fwrite(&Doors[i].YEntrance,sizeof(u8),1,fp);
		//Doors[i].DHeight+=Doors[i].YEntrance/16;
		// h = Doors[i].DHeight+Doors[i].YEntrance-1;
		fwrite(&Doors[i].DHeight,sizeof(u8),1,fp);
		
		fwrite(&Doors[i].DestDoor,sizeof(u8),1,fp);
		fwrite(&Doors[i].ExitDoorLength,sizeof(u8),1,fp);
		fwrite(&Doors[i].u1,sizeof(u8),1,fp);
		fwrite(&Doors[i].u2,sizeof(u8),1,fp);
		fwrite(&Doors[i].u3,sizeof(u8),1,fp);
		fwrite(&Doors[i].u4,sizeof(u8),1,fp);
	}
	//fclose(GBA.ROM);
	//GBA.ROM = fopen(GBA.FileLoc,"r+bw+b");
	
	fclose(fp);
	return 0;
}

int ConnectDoor(u8 TD){
	char Buf[512];
	u8 condoor = Doors[TD].DestDoor;
	
	sprintf(Buf,"Door: %x",TD);
	SetWindowText(GetDlgItem(DoorWin,lblCDoor),Buf);
	
	condoor = Doors[Doors[TD].DestDoor].DoorType;
	sprintf(Buf,"%X",condoor);
	SetWindowText(GetDlgItem(DoorWin,lblDoorType),Buf);
	
	condoor = Doors[Doors[TD].DestDoor].OwnerRoom;
	sprintf(Buf,"%X",condoor);
	SetWindowText(GetDlgItem(DoorWin,lblOwnerRoom),Buf);
	
	condoor = Doors[Doors[TD].DestDoor].DWidth;
	sprintf(Buf,"%d",condoor);
	SetWindowText(GetDlgItem(DoorWin,lblCDWidth),Buf);
	
	/* u8 DoorType;
	u8 OwnerRoom;
	u8 XEntrance;
	u8 DWidth;
	u8 YEntrance;
	u8 DHeight;
	u8 DestDoor;
	u8 ExitDoorLength;
	u8 u1;
	u8 u2;
	u8 u3;
	u8 u4;*/
	return 0;
}

